package com.firebird.app.fekpsl.ui.fragment.sword.rank

import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.adapter.sword.rank.LevelScoreRankAdapter
import com.firebird.app.fekpsl.base.BaseMvpFragment
import com.firebird.app.fekpsl.bean.*
import com.firebird.app.fekpsl.mvp.sword.rank.LevelScoreWorldRankContract
import com.firebird.app.fekpsl.mvp.sword.rank.LevelScoreWorldRankPresenter
import com.firebird.app.fekpsl.widget.BubblePopupWindow.BubblePopupWindow
import com.firebird.app.fekpsl.widget.SpaceItemDecoration
import kotlinx.android.synthetic.main.sword_fragment_member_rand_world.*

/**
 * A simple [Fragment] subclass.
 * Use the [MemberRandWorldFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MemberRandWorldFragment : BaseMvpFragment<LevelScoreWorldRankContract.View, LevelScoreWorldRankContract.Presenter>(), LevelScoreWorldRankContract.View {

    companion object{
        fun getInstance(): MemberRandWorldFragment = MemberRandWorldFragment()
    }

    override fun createPresenter(): LevelScoreWorldRankContract.Presenter = LevelScoreWorldRankPresenter()

    private var rightBottomWindow: BubblePopupWindow? = null
    var inflater: LayoutInflater? = null

    private val datas = mutableListOf<LevelScore>()

    private val recyclerViewItemDecoration by lazy {
        activity?.let {
            SpaceItemDecoration(it)
        }
    }

    private val levelScoreRankAdapter: LevelScoreRankAdapter by lazy {
        LevelScoreRankAdapter(activity, datas)
    }
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(activity)
    }
    private var isRefresh = true
    override fun attachLayoutRes(): Int = R.layout.sword_fragment_member_rand_world
    override fun initView(view: View) {
        super.initView(view)
        swipeRefreshLayout.run {
            setOnRefreshListener(onRefreshListener)
        }
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = levelScoreRankAdapter
            itemAnimator = DefaultItemAnimator()
            recyclerViewItemDecoration?.let { addItemDecoration(it) }
        }

        levelScoreRankAdapter.run {
            bindToRecyclerView(recyclerView)
            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@MemberRandWorldFragment.onItemClickListener
            onItemChildClickListener = this@MemberRandWorldFragment.onItemChildClickListener
        }
    }

    override fun lazyLoad() {
        mPresenter?.getLevelScoreWorldRankList(0)
    }

    override fun showLoading() {
        // swipeRefreshLayout.isRefreshing = isRefresh
    }

    override fun hideLoading() {
        swipeRefreshLayout?.isRefreshing = false
        if (isRefresh) {
            levelScoreRankAdapter.run {
                setEnableLoadMore(true)
            }
        }
    }

    override fun showError(errorMsg: String) {
        super.showError(errorMsg)
        levelScoreRankAdapter.run {
            if (isRefresh)
                setEnableLoadMore(true)
            else
                loadMoreFail()
        }
    }

    override fun showLevelScoreWorldRankList(body: BaseListResponseBody<LevelScore>) {
        body.datas.let {
            levelScoreRankAdapter.run {
                if (isRefresh) {
                    replaceData(it)
                } else {
                    addData(it)
                }
                val size = it.size
                if (size < body.size) {
                    loadMoreEnd(isRefresh)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }

    /**
     * RefreshListener
     */
    private val onRefreshListener = SwipeRefreshLayout.OnRefreshListener {
        isRefresh = true
        levelScoreRankAdapter.setEnableLoadMore(false)
        mPresenter?.getLevelScoreWorldRankList(0)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        isRefresh = false
        swipeRefreshLayout.isRefreshing = false
        val page = levelScoreRankAdapter.data.size / 10
        mPresenter?.getLevelScoreWorldRankList(page)
    }

    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas.size != 0) {

        }
    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener =
            BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
                if (datas.size != 0) {
                    val data = datas[position]
                    when (view.id) {
                        R.id.iv_wall_avatar->{
                            rightBottomWindow =  BubblePopupWindow(this.context)

                            val bubbleView: View = LayoutInflater.from(this.context).inflate(R.layout.layout_popup_view, null)
                            //rootView.addView(insideView)

                            // val bubbleView: View = inflater!!.inflate(R.layout.layout_popup_view,null)
                            rightBottomWindow!!.setBubbleView(bubbleView)
                            rightBottomWindow!!.show(view, Gravity.RIGHT, 0f)

                        }
                    }
                }
            }

}